Redirection based on language

I have two web sites under this domain, the one in Turkish is located at /tr and the one in English is located at /en path. These are separated WordPress installations, not in a network. I had to make a choice for my visitors reaching directly to my domain root. Welcoming them with a static language choice page was an option, but this was not a good idea in my opinion.

I have searched for some time to find an answer to this question: How could I seamlessly and automatically redirect my visitors to my WordPress sites located on my domain’s subfolders based on their preferred language, by avoiding explicitly forcing them to choose a language when they reach my domain?

I will shortly explain here how I have successfully set this configuration up with just two files.

I use this index.php file at / (root):

<?php
// Source: http://stackoverflow.com/questions/3770513/detect-browser-language-in-php
// Modified for WordPress by Ertuğrul Harman @ ertugrulharman.com
// 20.03.2016
define('WP_USE_THEMES', true);
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
    case "tr":
        //echo "PAGE TR";
//        include("/tr/index.php");//include check session TR
    require( dirname( __FILE__ ) . '/tr/wp-blog-header.php' );
        break;
    case "en":
        //echo "PAGE EN";
//        include("en/index.php");
    require( dirname( __FILE__ ) . '/en/wp-blog-header.php' );
        break;        
    default:
        //echo "PAGE EN - Setting Default";
    require( dirname( __FILE__ ) . '/en/wp-blog-header.php' );
        //include("en/index.php");//include EN in all other cases of different lang detection
        break;
}
?>

And this .htaccess file at / (root):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

By using those two files and locating my web sites in their respective subfolders, my visitors accessing directly to “ertugrulharman.com” are silently being redirected to Turkish or English sites on this domain based on their language choices in their browsers (Firefox users may choose their preferred language in Preferences/Content/Language section, for example).

A little complicated scenario

You may use the configuration above as it is if you set new web sites up. My case was a little complicated. At first I had only my site in Turkish on my domain and it had been accessible for a long time. Therefore search engines had already indexed that web site with its address structure. I then moved this installation to “/tr” subfolder and created a new site in “/en” subfolder.

To keep existing links from third party sources such as search engines directly to my domain root (in other words, for backward compatibility), I used a different “.htaccess” file at / (root) path.

Modifying .htaccess file can be a complicated task for those who do not have enough experience. These codes are often used as they found around the web.

There is also a tendency to treat rewrite rules as magic incantation, using them without actually understanding what they do. (from Official Apache Documentation)

Making modifications according to my need could only be possible by understanding the code with the help of the documentation. If you do not have that time or interest you may take a quick look at this page.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /tr/index.php [L]
</IfModule>
# END WordPress

This “.htaccess” file redirects all files and directories which are not accessible on root to /tr.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /tr/index.php [L]

The block above provides this result.

For example, a request to “http://ertugrulharman.com/2016/03/05/whatsapp/” will be redirected to “http://ertugrulharman.com/tr/2016/03/05/whatsapp/”, as there is not any “2016” directory at root.

Changing “Site address” and “WordPress address” fields of “Admin/General” tab on the dashboard of my Turkish wordpress site as “ertugrulharman.com/tr” was also necessary.

Now both sites and automatic redirection perfectly work.

4 thoughts on “Redirection based on language

  1. Any problems with loading speed of the website with this?

    I have a redirect.php for WPML and it looks like my script needs over 2 seconds “more” to forward and loading the full website!

    Thanks!

    1. Hi Florian!

      Maybe one second. I am not sure. It is not easy to notice, especially when your server is fast.

  2. how can i write , select your language button on my wordpress website.
    i have install plugin for language but how to give button for selection of language ?

  3. Just attach the landing page of your respective WP installation as a link to the language button. For example, check target links of flag icons on this site at the upper-right corner.

Leave a Reply

Your email address will not be published. Required fields are marked *